package com.wwj.core.system.service.impl;

import com.wwj.core.system.entity.User;
import com.wwj.core.system.entity.UserRole;
import com.wwj.core.system.entity.mapper.RoleMapper;
import com.wwj.core.system.entity.mapper.UserMapper;
import com.wwj.core.system.entity.mapper.UserRoleMapper;
import com.wwj.core.system.service.IUserService;
import com.wwj.utils.encrypt.EncryptUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xya on 16-10-24.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;


    @Override
    public User findLoginUser(String account) {
        return userMapper.selectLoginUser(account);
    }

    @Override
    public Page findUserByPage(int pageNo, int pageSize, String search) throws Exception {
        Map<String, Object> params = null;
        if (null != search && !"".equals(search.trim())) {
            params = new HashMap<String, Object>(1);
            params.put("search", search);
        }
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userMapper.selectUser(params);
        return (Page) list;
    }

    @Override
    public int createUser(User user) throws Exception {
        //密码加密
        user.setPassword(EncryptUtil.getInstance().MD5(user.getPassword()));
        int num = userMapper.save(user);
        if (null != user.getRoleIds()) {
            for (Integer roleId : user.getRoleIds()) {
                UserRole ur = new UserRole(user.getId(), roleId);
                userRoleMapper.save(ur);
            }
        }
        return num;
    }


    @Override
    public int deleteUser(String id) throws Exception {
        return userMapper.update4Delete(id);
    }

    @Override
    public int updateUser(User user) throws Exception {
        User persist = userMapper.getById(user.getId());
        persist.setName(user.getName());
        if (null != user.getPassword() && !"".equals(user.getPassword()))
            persist.setPassword(EncryptUtil.getInstance().MD5(user.getPassword()));
        persist.setAccount(user.getAccount());
        persist.setStatus(user.getStatus());
        //persist.setEmployeeNo(user.getEmployeeNo());
        persist.setPhone(user.getPhone());
        persist.setIsSuperAdmin(user.getIsSuperAdmin());
        persist.setUpdateTime(new Date());

        //清空用户权限
        userRoleMapper.deleteRoleByUserId(user.getId());
        //重新赋权
        for (Integer roleId : user.getRoleIds()) {
            UserRole ur = new UserRole(user.getId(), roleId);
            userRoleMapper.save(ur);
        }
        return userMapper.update(persist);
    }

    @Override
    public int updatePwd(User user) throws Exception {
        return userMapper.updatePwd(user);
    }

  /*  @Override
    public boolean validateEmployeeNo(String employeeNo) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("employeeNo", employeeNo);
        return userMapper.validateEmployeeNo(params) > 0;
    }*/

    @Override
    public User findUserById(String id) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("id", id);
        List<User> users = userMapper.selectUser(params);
        return (null != users && users.size() == 1) ? users.get(0) : null;
    }

    @Override
    public boolean checkUserPhone(String phone) throws Exception {
        return userMapper.checkUserPhone(phone) > 0;
    }



}
